package sample.controls;

import org.dreamwork.jasmine2.engine.IPostbackHandler;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.util.DataBinder;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.CheckBox;
import org.dreamwork.jasmine2.web.controls.Panel;
import org.dreamwork.jasmine2.web.controls.Radio;
import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.jasmine2.web.html.Table;
import org.dreamwork.jasmine2.web.html.TableCell;
import org.dreamwork.jasmine2.web.html.TableRow;
import org.dreamwork.util.NameValueCollection;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 12-3-19
 * Time: 下午9:19
 *
 * <table>
 *  <tr>
 *      <td><input type='text' /></td>
 * </tr>
 * </table>
 */
public class MyControl extends Panel implements IPostbackHandler {
    private Table table;

    @Override
    public void init () {
        super.init ();
    }

    @Override
    public void render (PrintWriter writer) throws IOException, EventException {
        super.render (writer);
    }

    @Override
    protected void beginTag (PrintWriter writer) throws IOException {
        super.beginTag (writer);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void endTag (PrintWriter writer) {
        super.endTag (writer);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void dataBind () throws Throwable {
        super.dataBind ();

//        System.out.println ("-------------------" + this.dataSource + "------------------");

        this.createChildControls ();
        this.childControlCreated = true;
    }

    @Override
    public void createChildControls () {
        List<Map<String, String>> list = (List<Map<String, String>>) this.dataSource;
        if (list == null) return;

//        Map<String, String> map = list.get (0);

        table = new Table ();
        this.addControl (table);

/*
        List<String> header = new ArrayList<String> (map.size ());
        for (String key : map.keySet ()) header.add (key);
*/

/*
        TableRow tr = new TableRow ();
        table.addRow (tr);
        for (String name : header) {
            TableCell td = new TableCell (true);
            td.setInnerHTML (name);
            tr.addCell (td);
        }
*/

        int index = 1;
        for (Map<String, String> map : list) {
            TableRow tr = new TableRow ();
            table.addRow (tr);
            TableCell td = new TableCell ();
            tr.addCell (td);

            String title = map.get ("title");
            String type = map.get ("type");
            String value = map.get ("value");

            String[] a = null;
            if (backup != null) {
                String s = backup.get ("CC");
                if (s != null)
                    a = s.split (",");
            }

            IWebControl c = null;
            if ("checkbox".equalsIgnoreCase (type)) {
                CheckBox box = new CheckBox ();
                box.setText (title);
                box.setValue (value);
                if (a != null) for (String s : a) {
                    if (value.equals (s))
                        box.setChecked (true);
                }
                c = box;
            } else if ("radio".equalsIgnoreCase (type)) {
                Radio radio = new Radio ();
                radio.setText (title);
                c = radio;
            }
            c.setId ("c" + index);
            c.setName ("cc");

            td.addControl (c);

            index ++;
        }
    }

    private Map<String, String> backup;

    public void postbackData (NameValueCollection props) {
        if (page.isPostback ())  {
            backup = new HashMap<String, String> (props);
            backup.putAll (props);
        }
    }
}
